package com.immediatus.engine.input;

import android.view.MotionEvent;
import com.immediatus.util.GenericPool;

public class TouchEvent{
    public static final int ACTION_CANCEL = MotionEvent.ACTION_CANCEL;
    public static final int ACTION_DOWN = MotionEvent.ACTION_DOWN;
    public static final int ACTION_MOVE = MotionEvent.ACTION_MOVE;
    public static final int ACTION_OUTSIDE = MotionEvent.ACTION_OUTSIDE;
    public static final int ACTION_UP = MotionEvent.ACTION_UP;

    private static final TouchEventPool TOUCHEVENT_POOL = new TouchEventPool();

    protected int _pointerID;

    protected float _x;
    protected float _y;

    protected int _action;

    protected MotionEvent _motionEvent;

    public static TouchEvent obtain(final float x_, final float y_, final int action_, final int pointerID_, final MotionEvent motionEvent_){
        final TouchEvent touchEvent = TOUCHEVENT_POOL.obtainPoolItem();
        touchEvent.set(x_, y_, action_, pointerID_, motionEvent_);
        return touchEvent;
    }

    private void set(final float x_, final float y_, final int action_, final int pointerID_, final MotionEvent motionEvent_){
        this._x = x_;
        this._y = y_;
        this._action = action_;
        this._pointerID = pointerID_;
        this._motionEvent = motionEvent_;
    }

    public void recycle(){
        TOUCHEVENT_POOL.recyclePoolItem(this);
    }

    public static void recycle(final TouchEvent touchEvent_){
        TOUCHEVENT_POOL.recyclePoolItem(touchEvent_);
    }

    public float getX(){
        return this._x;
    }

    public float getY(){
        return this._y;
    }

    public void set(final float x_, final float y_){
        this._x = x_;
        this._y = y_;
    }

    public void offset(final float dx_, final float dy_){
        this._x += dx_;
        this._y += dy_;
    }

    public int getPointerID(){
        return this._pointerID;
    }

    public int getAction(){
        return this._action;
    }

    public boolean isActionDown(){
        return this._action == TouchEvent.ACTION_DOWN;
    }

    public boolean isActionUp(){
        return this._action == TouchEvent.ACTION_UP;
    }

    public boolean isActionMove(){
        return this._action == TouchEvent.ACTION_MOVE;
    }

    public boolean isActionCancel(){
        return this._action == TouchEvent.ACTION_CANCEL;
    }

    public boolean isActionOutside(){
        return this._action == TouchEvent.ACTION_OUTSIDE;
    }

    public MotionEvent getMotionEvent(){
        return this._motionEvent;
    }

    private static final class TouchEventPool extends GenericPool<TouchEvent>{
        @Override
        protected TouchEvent onAllocatePoolItem(){
            return new TouchEvent();
        }
    }
}
